import wx

import helper
import options as opt


class MyFrame(wx.Frame):

    def __init__(self, cq):
        wx.Frame.__init__(self, None, -1, '消息发送窗口', size=(300, 250))
        self.cq = cq
        self.re_msg = {}
        self.g_opt = opt.GROUP_OPTIONS
        panel = wx.Panel(self, -1)
        # panel.SetBackgroundColour('white')

        # 列表框
        list_str_group = helper.list_group_desc()
        self.ch1 = wx.ComboBox(panel, -1, value='选择QQ群', choices=list_str_group)
        # self.Bind(wx.EVT_COMBOBOX,self.on_combobox,self.ch1)
        self.ch1.Bind(wx.EVT_COMBOBOX, self.on_combobox)

        # 输入框
        self.txt_msg = wx.TextCtrl(panel, -1, size=(200, 160), style=wx.TE_MULTILINE)

        # 创建一个按钮
        self.button_fresh = wx.Button(panel, -1, label='刷新', size=(60, 20), pos=(10, 180))
        self.button_fresh.Bind(wx.EVT_BUTTON, self.on_btn_fresh)

        # 文本
        self.lbl_info = wx.StaticText(panel, -1, '', pos=(10, 160))
        self.lbl_info.SetForegroundColour('red')

        # 创建一个按钮
        self.button_ok = wx.Button(panel, -1, label='发送', size=(60, 20), pos=(10, 180))
        self.button_ok.Bind(wx.EVT_BUTTON, self.on_btn_ok)

        # 创建一个按钮
        self.button_close = wx.Button(panel, -1, label='关闭', size=(60, 20), pos=(70, 180))
        self.button_close.Bind(wx.EVT_BUTTON, self.on_btn_close)

        boxsizer = wx.BoxSizer(wx.VERTICAL)
        boxszer_h1 = wx.BoxSizer(wx.HORIZONTAL)
        boxszer_h1.Add(self.ch1, proportion=0)
        boxszer_h1.Add(self.button_fresh, 0, wx.EXPAND, 2)
        boxsizer.Add(boxszer_h1, border=1)
        boxsizer.Add((-1, 10))
        boxsizer.Add(self.txt_msg, 3, wx.EXPAND, border=10)

        boxszer_h = wx.BoxSizer(wx.HORIZONTAL)
        boxszer_h.Add(self.button_ok, proportion=1)
        boxszer_h.Add(self.button_close, proportion=1)
        boxszer_h.Add(self.lbl_info, proportion=1)
        boxsizer.Add(boxszer_h, proportion=1)
        panel.SetSizer(boxsizer)

    def on_btn_ok(self, event):
        idx = self.ch1.Selection
        if idx < 0:
            self.lbl_info.SetLabelText('请先选择QQ群')
            return
        group_id = opt.GROUP_OPTIONS[idx]['id']
        self.cq.api.send_group_msg(group_id, self.txt_msg.GetValue())

    def on_btn_fresh(self, event):
        self.lbl_info.SetLabelText('暂不可用')

    def on_btn_close(self, event):
        # wx.Exit()
        self.Close()

    def on_combobox(self, event):
        idx = self.ch1.Selection
        group_id = opt.GROUP_OPTIONS[idx]['id']
        cfg = (helper.read_gourp_cfg(group_id))

        self.lbl_info.SetLabelText('')

        print(group_id)
        print("选择{0}".format(str(cfg)))


def show(cq):
    app = wx.App()
    frame = MyFrame(cq)
    frame.Show()
    app.MainLoop()

# if __name__ == '__main__':
#     show('code.jpg')
